package com.kbxwn.logmanager.entity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kbxwn.logmanager.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>描述：
 * <p>作者：liangguokui 2018/3/20
 */
public class HistoryTagAdapter extends BaseAdapter {

    public static final String KEY = "history_tags";
    public static final String SPILT_KEY = "&##&";

    private Context context;

    private List<String> historyTags;

    public HistoryTagAdapter(Context context) {
        this.context = context;

        String tags = PreferenceUtils.getString(context, KEY, "");
        if (tags.length() == 0) {
            historyTags = new ArrayList<>();
        } else {
            String[] temp = tags.split(SPILT_KEY);
            historyTags = new ArrayList<>(Arrays.asList(temp));
        }
    }

    @Override
    public int getCount() {
        return historyTags.size();
    }

    @Override
    public String getItem(int position) {
        return historyTags.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_history_tag, null);
        }

        TextView tvTag = ViewHolder.get(convertView, R.id.tv_tag);
        tvTag.setText(historyTags.get(position));
        ViewHolder.get(convertView, R.id.v_item_click).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener != null) {
                    onClickListener.onItemClick(historyTags.get(position));
                }
            }
        });

        ViewHolder.get(convertView, R.id.imgv_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                historyTags.remove(position);
                notifyDataSetChanged();
            }
        });

        return convertView;
    }

    public void addTag(String tag) {
        if (historyTags.contains(tag)) {
        } else {
            historyTags.add(tag);
            notifyDataSetChanged();
        }
    }

    public void onSave() {
        StringBuilder builder = new StringBuilder();
        builder.append(historyTags.get(0));
        for (int i = 1; i < historyTags.size(); i++) {
            builder.append(SPILT_KEY);
            builder.append(historyTags.get(i));
        }

        PreferenceUtils.putString(context, KEY, builder.toString());
    }

    private OnClickItemListener onClickListener;

    public void setOnClickListener(OnClickItemListener onClickListener) {
        this.onClickListener = onClickListener;
    }

    public interface OnClickItemListener {
        void onItemClick(String tag);
    }
}
